<template>
  <el-drawer
    :title="dialogTilte"
    :before-close="handleClose"
    :visible.sync="showDialog"
    direction="rtl"
    custom-class="drawer-industry-new-modify"
    ref="drawer"
    :wrapperClosable="false"
  >
    <div class="drawer-body">
      <el-form
        ref="form"
        :model="formData"
        label-position="right"
        label-width="150px"
      >
        <el-form-item
          prop="mid_table_remark_008"
          label="行业ID"
          :rules="[{ required: true, message: '请输入行业名称' }]"
        >
          <el-input
            v-model="formData.mid_table_remark_008"
            autocomplete="off"
            placeholder="请输入行业名称"
            :disabled="true"
          ></el-input>
        </el-form-item>
        <el-form-item
          prop="mid_table_remark_009"
          label="行业名称"
          :rules="[{ required: true, message: '请输入行业名称' }]"
        >
          <el-input
            v-model="formData.mid_table_remark_009"
            autocomplete="off"
            placeholder="请输入行业名称"
            :disabled="true"
          ></el-input>
        </el-form-item>
        <el-form-item
          prop="mid_table_remark_021"
          label="行业规则"
          :rules="[{ required: true, message: '请输入行业规则' }]"
        >
          <el-input
            type="textarea"
            v-model="formData.mid_table_remark_021"
            autocomplete="off"
            placeholder="请输入行业规则"
            :autosize="{ minRows: 5, maxRows: 8 }"
            resize="none"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="handleClose">取消</el-button>
          <el-button type="primary" :loading="loading" @click="handleSubmit">{{
            loading ? "提交中 ..." : "确定"
          }}</el-button>
        </el-form-item>
      </el-form>
    </div>
  </el-drawer>
</template>
<script>
import { Base64 } from "js-base64";
import { parseTime } from "@/utils/index";
import { middleList } from "@/api/middle";
export default {
  name: "CompIndustryNewModify",
  data() {
    return {
      showDialog: false,
      formData: {
        // 表ID
        pre_db_para_id_public_middle_table: "",
        mid_table_remark_008: "",
        mid_table_remark_009: "",
        mid_table_remark_021: "",
      },
      loading: false,
    };
  },
  computed:{
    dialogTilte(){
      if(this.formData.pre_db_para_id_public_middle_table||this.formData.pre_db_para_id_public_middle_table===0){
        return "编辑规则"
      }
      return "新增规则"
    }
  },
  props: {},
  methods: {
    onEditOrAdd(row) {
      if(!row.mid_table_remark_008){
        return;
      }
      let tmp021="";
      if(row.mid_table_remark_021){
        try {
          tmp021= Base64.decode(row.mid_table_remark_021)
        } catch (error) {
          tmp021=row.mid_table_remark_021
        }

      }
      this.formData = {
        // 表ID
        pre_db_para_id_public_middle_table: row.id_public_middle_table || "",

        mid_table_remark_008: row.mid_table_remark_008,
        mid_table_remark_009: row.mid_table_remark_009,
        mid_table_remark_021:tmp021,
      };
      this.showDialog = true;
      this.$nextTick(() => {
        this.$refs.form.clearValidate();
      });
    },
    handleClose() {
      this.showDialog = false;
      if (this.loading) {
        this.$message.error("数据提交中,暂不能关闭窗口");
      }
    },
    handleSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.onSubmit();
        }
      });
    },
    onSubmit() {
      if (this.loading) {
        return;
      }
      this.loading = true;
      let postData = {
        invoke_method: "public_insert_or_update_middle_table",
        pre_db_para_middle_table_name:
          "public_middle_table_lite_mod_ai_chatgpt",
        type_middle_app: 20001,
        type_inner_app: 1001,
        is_online: 0,
        mid_table_remark_008: this.formData.mid_table_remark_008,
        mid_table_remark_009: this.formData.mid_table_remark_009,
        mid_table_remark_021: this.formData.mid_table_remark_021,
      };
      try {
        postData.mid_table_remark_021=Base64.encode(this.formData.mid_table_remark_021);
      } catch (error) {
        console.log("rule>>>>>>>",error)
      }

      if (
        this.formData.pre_db_para_id_public_middle_table ||
        this.formData.pre_db_para_id_public_middle_table === 0
      ) {
        postData.pre_db_para_id_public_middle_table =
          this.formData.pre_db_para_id_public_middle_table;
      } else {
        let dtNow = new Date();
        Object.assign(postData, {
          mid_table_remark_007: parseTime(dtNow, "{y}{m}{d}"),
          mid_table_remark_006: parseTime(dtNow, "{y}{m}"),
          mid_table_remark_005: parseTime(dtNow, "{y}"),
        });
      }
      middleList(this.Qs.stringify(postData)).then(
        (res) => {
          this.$notify({
            title: "Success",
            message: "保存成功",
            type: "success",
            duration: 2000,
          });
          this.$emit("refresh");
          this.showDialog = false;
          this.loading=false
        },
        (error) => {
          this.loading=false
        }
      );
    },
  },
};
</script>
<style lang="scss" scoped>
.drawer-industry-new-modify {
  width: 700px;
  padding-right: 10rpx;
  box-sizing: border-box;

  .drawer-body {
    position: relative;
    width: 100%;
    padding-left: 30px;
    padding-right: 30px;
    box-sizing: border-box;
  }
}
</style>
